﻿/**
 * ...
 * @author 
 */

package list;

import flash.events.MouseEvent;
import gui.hug.HUGSprite;
import gui.hug.HUGColors;
import list.NewList;
import gui.hug.Utils;


class FriendList extends NewList
{
	private var friendList : NewList;
	private var contentF : Array<FriendPanel>;
	private var closeButton : MyLinkButton;
	static private inline var SPACE_BETWEEN_ITEMS : UInt = 5;
	static private inline var HEIGHT : UInt = 450;
	static private inline var BACKGROUND_COLOR : UInt = HUGColors.WHITE;
	static private inline var BORDER_COLOR : UInt = Utils.SEL_BG_COL + 0x222222;
	static private inline var MB_COLOR : UInt = HUGColors.VK_BORDER_INNER - 0x222211;
	
	public function new() 
	{
		contentF = new Array();
		super(200, 0, 1, 225);
		
		Utils.rect(this, 0, 0, 225, HEIGHT, BACKGROUND_COLOR, BORDER_COLOR);
		Utils.rect(this, 0, 0, 225, 25, MB_COLOR , BORDER_COLOR);
		
		closeButton = new MyLinkButton();
		closeButton.setNewText("Закрыть");
		closeButton.setLocation(3, 3);
		closeButton.visible = true;
		closeButton.addEventListener(MouseEvent.CLICK, onClose);
		this.addChild(closeButton);
	}
	
	
	private override function drawContent() {
		if ( getQuantity() > 0 ) {
			var currentHeight : Float = 30;
			var end = getLastOnScreen() + 1;
			for ( index in firstShownItem ... end ) {
				if(contentF[index] != null ) {
					// drawing itemPanel
					contentF[index].setLocation(SPACE_BETWEEN_ITEMS, currentHeight );
					
					addChild(contentF[index]);
					var aditional = contentF[index].getHeight();
					currentHeight += aditional;
					
					
					// filling space between the items
					var space : HUGSprite = new HUGSprite(SPACE_BETWEEN_ITEMS, currentHeight );
					Utils.fillRect( space, -1, 0, w - W - 2 * SPACE_BETWEEN_ITEMS  + 2, SPACE_BETWEEN_ITEMS, HUGColors.WHITE );
					addChild(space);
					if ( aditional != 0 ) currentHeight += SPACE_BETWEEN_ITEMS;
				    
					wid = this.width;
				}
			}
			
			// filling the space that is left after all the items are drawn
			var space : HUGSprite = new HUGSprite(SPACE_BETWEEN_ITEMS , currentHeight -5);
			
			Utils.fillRect( space, -1, 0, w - W - 2 * SPACE_BETWEEN_ITEMS + 2, HEIGHT - currentHeight , HUGColors.WHITE );
			addChild(space);
		}
	}
	
	private override function getLastOnScreen() : UInt {
		var result = firstShownItem;
		var currentHeight : Float = 0;
		
		for ( i in firstShownItem ... getQuantity() ) {
			currentHeight += contentF[i].getHeight() == 0 ?  0 : (contentF[i].getHeight() + SPACE_BETWEEN_ITEMS);
			if ( currentHeight > HEIGHT ) return result;
			result ++ ;
		}
		
		return result;
	}
	
	public override function getLength( begin : UInt = 0,  ?end : UInt ) : Float {
		if ( end == null ) end = getQuantity();
		var currentHeight : Float = 0;
		var index : UInt = begin;
		while ( begin < end ) {
			currentHeight += contentF[begin].getHeight() + SPACE_BETWEEN_ITEMS;
			begin ++ ;
		}
		
		return currentHeight;
	}
	
	private function onClose (e : MouseEvent) {
		this.visible = false;
	}
	

	
	public function addFriend( name : String, id : Int, item : Int ) {
		contentF.push( new FriendPanel( name,id, item) ) ;
		draw();
	}
	
	public override function getQuantity() : UInt{
		return contentF.length; }
}